#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>



int main(int argc,char *argv[]){
    //pthread_mutex_init,set process_shared
    pthread_mutex_t lock;
    pthread_mutexattr_t mutexattr;
    pthread_mutexattr_init(&mutexattr);
    pthread_mutexattr_setpshared(&mutexattr,PTHREAD_PROCESS_SHARED);
    pthread_mutex_init(&lock,&mutexattr);


    pid_t pid;
    int status;
    pid = fork();
    if(pid < 0)
    {
        perror("fork is error\n");
        exit(EXIT_FAILURE);
    }else if(pid == 0){
        //child process get the mutex lock
        pthread_mutex_lock(&lock);

        //know who first get the lock
        printf("child lock:pid = %d,ppid = %d\n",getpid(),getppid());
        sleep(5);
        pthread_mutex_unlock(&lock);
        printf("child unlock:pid = %d,ppid = %d\n",getpid(),getppid());
    }else{
        //father process get the mutex lock
        pthread_mutex_lock(&lock);

        //know who first get the lock
        printf("father lock:pid = %d,ppid = %d\n",getpid(),getppid());
        sleep(5);
        pthread_mutex_unlock(&lock);
        printf("father unlock:pid = %d,ppid = %d\n",getpid(),getppid());
        wait(&status);
        if(WIFEXITED(status))
        {
            printf("child is finish,status: %s\n",WEXITSTATUS(status));
        }
        if(WIFSIGNALED(status))
        {
            printf("child is finish,status: %s\n",WTERMSIG(status));
        }
    }

    return 0;
}
/**
this code show: parent process and child process get one lock at same time ,but pthread_mutexattr_setpshared function what describe one process get one lock,the code is wrong.**/